<?php
/*
 *      nwftable.php
 *      
 *      Copyright 2010 Rakitin Vitaliy <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class nwftable {
	
	private $dataset;
	private $uuid;
	
	function __construct($uuid, $dataset) {
		$this->uuid = $uuid;
		$_SESSION[$uuid] = &$this;
		$this->dataset = &$dataset;
		$this->dataset->registerme($this->uuid);
	}
	
	function get() {
		
		$out = "<table id='$this->uuid' style='border: solid black 1px;'>";
		
		$result = $this->dataset->getdata();
		$header = $this->dataset->getheader();
		
		$out .= "<tr>";
		foreach($header as $cell) {
			$id = $cell["id"];
			$name = $cell["name"];
			if ($this->dataset->orderby == $id) {
				if ($this->dataset->direction == "ASC") {
					$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name ASC</th>";
				} else {
					$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name DESC</th>";
				}
			} else {
				$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name</th>";
			}
		}
		$out .= "</tr>";

		$select = $this->dataset->select;
		foreach($result as $row) {
			if ($row["id"] != $select) {
				$out .= "<tr style='cursor:pointer;background-color:#FFFFFF;color:#000000;' onclick=message('$this->uuid','select','".$row["id"]."',0);>";
			} else {
				$out .= "<tr style='cursor:pointer;background-color:#B9C7D2;color:#000000;' onclick=message('$this->uuid','select','".$row["id"]."',0);>";
			}
			
			foreach($header as $cell) {
				$out .= "<td style='border: solid black 1px;'>";
				switch ($cell["type"]) {
					case "varchar": {$out .= $row[$cell["id"]]; break;}
					case "link": {
						$field = $GLOBALS["metadata"]->objects->byuuid($this->dataset->uuid)->items->byuuid($row["id"])->cells->byuuid($cell["id"]);
						$out .= $GLOBALS["metadata"]->objects->byuuid($field->luobject)->items->byuuid($field->value)->cells->byuuid($field->lufield)->value;
						break;
					}
				}
				$out .= "</td>";
			}
			$out .= "</tr>";
		}
		$out .= "</table>";
		return $out;
	}
	
	function orderby($name) {
		if ($this->dataset->orderby == $name) {
			if ($this->dataset->direction == "ASC") {
				$this->dataset->direction = "DESC";
			} else {
				$this->dataset->direction = "ASC";
			}
		} else {
			$this->dataset->orderby = $name;
			$this->dataset->direction = "ASC";
		}
		$this->update();
	}
	
	function select($uuid) {
		$this->dataset->select = $uuid;
	}
	
	function update() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('update',$this->uuid,$this->get());
	}
}
?>
